﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: awinkler2
 * Datum: 03.08.2010
 * Zeit: 21:52
 *
 * I try to change you baby.
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.IO;

using export.Database;
using log4net;
using log4net.Config;
using MySql;
using MySql.Data.MySqlClient;
using Spring.Context;
using Spring.Context.Support;
using Spring.Core;
using Spring.Data;
using Spring.Data.Common;

namespace export
{
    class Export
    {
    	private static readonly ILog LOG = LogManager.GetLogger(typeof (Export));

        private MySqlConnection conn;

        public void ShowMatches()
        {
            string commandString = @"
                select
                    g.bo_date, home.bo_name as home_team, guest.bo_name as guest_team,
                    g.bo_homegoals as home_goals, g.bo_guestgoals as guest_goals
                from
                    bo_gamelist gl, bo_game g, bo_team home, bo_team guest
                where
                    gl.bo_season_ref = 15
                    and g.bo_gamelist_ref = gl.id
                    and g.bo_hometeam_ref = home.id
                    and g.bo_guestteam_ref = guest.id
                order by
                    gl.bo_index, g.bo_index";
            //string connectionString = "server=localhost;uid=sportwetten;pwd=sportwetten;database=piratesproduction;";

            //SqlDataAdapter A = new SqlDataAdapter()
            MySqlDataAdapter DataAdapter = new MySqlDataAdapter();
            DataAdapter.SelectCommand = new MySqlCommand(commandString, conn);

            DataSet DataSet = new DataSet();
            DataAdapter.Fill(DataSet, "Matches");
            DataTable dataTable = DataSet.Tables[0];

            foreach (DataRow dataRow in dataTable.Rows)
            {
                System.Console.WriteLine(
                    dataRow["home_team"] + " - " + dataRow["guest_team"] + " " + dataRow["home_goals"] + ":" + dataRow["guest_goals"] );
            }
        }

        public static void Main(string[] args)
        {
        	XmlConfigurator.Configure();
        	IApplicationContext ctx = ContextRegistry.GetContext();
        	DatabaseConnection conn = (DatabaseConnection) ctx.GetObject("DatabaseConnection");
        	CptasklistDAO cptasklistDao = (CptasklistDAO) ctx.GetObject("cptasklistDAO");

        	LOG.Debug("We are here!");
        	cptasklistDao.FindAll();

            Export program = new Export();
            program.conn = conn.GetConnection();

            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = "bo_season";
            cmd.Connection = program.conn;
            cmd.CommandType = CommandType.TableDirect;

            IDataReader reader = cmd.ExecuteReader();

            char seperator = '|';

            using (StreamWriter sw = new StreamWriter(@"D:\temp\export.csv", false, System.Text.Encoding.Default))
            {
	            while (reader.Read())
	            {
	            	for (int i = 0; i < reader.FieldCount; i++)
	            	{
		            	Console.WriteLine("Name: {0}, {1}", i, reader[i].GetType().Name);
						sw.Write(reader[i].ToString());
		            	if (i < reader.FieldCount - 1)
		            	{
		            		sw.Write(seperator);
		            	}
		            	else
		            	{
		            		sw.WriteLine("");
		            	}
	            	}
	            }
            	reader.Close();
            }
            
            program.ShowMatches();
            
            Console.Write("Press any key to continue ...");
            Console.ReadKey(true);
        }
    }
}
